package com.example.jiaju.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.jiaju.R;
import com.example.jiaju.entity.House;
import com.example.jiaju.ui.main.ConcreteHouseActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class NewHouseAdapter extends RecyclerView.Adapter<NewHouseAdapter.MyViewHolder> {

    private String TAG="NewHouseAdapter";
    private List<House.PageDTO.ListDTO> list;

    private Context context;
    public NewHouseAdapter(Activity context,List<House.PageDTO.ListDTO> list) {
        this.context=context;
        this.list=list;
    }

    //     设置RecyclerView中的内容
    /*加载并返回布局文件   java->xml*/
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.house_item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    /*填充，修改布局里的控件内容*/
    /*1.可以将数据提前填充到一个数组中
     * 2.通过数组的下标获取到值，填充到控件中*/
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        House.PageDTO.ListDTO listDTO=list.get(position);
        if(listDTO==null){
            Log.e(TAG, "onBindViewHolder: listDTO is null");
        }else{
            holder.houseName.setText(listDTO.houseName);
            holder.houseAddress.setText(listDTO.houseAdd);
            holder.housePrice.setText(listDTO.housePrice.toString()+"元/平");
            Glide.with(context).load(listDTO.houseImg).into(holder.teaseImg);
            holder.houseLabel.setText(listDTO.houseLabel);
            //填充内容替换成数据接口请求
            holder.teaseImg.setOnClickListener(v -> {
                Intent intent=new Intent(context,ConcreteHouseActivity.class);
                intent.putExtra("houseListDTO", (Serializable) listDTO);
                context.startActivity(intent);
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    //    获取consult_item中控件
    static class MyViewHolder extends RecyclerView.ViewHolder{
        //命名对应consult实体类的变量名

        private ImageView teaseImg;
        private TextView houseName;
        private TextView housePrice;
        private TextView houseArea;
        private TextView houseState;
        private TextView houseLabel;
        private TextView housePhone;
        private TextView houseAddress;
        private ImageView houseModelImg;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            teaseImg=itemView.findViewById(R.id.tease_img);
            houseName=itemView.findViewById(R.id.house_text);
            housePrice=itemView.findViewById(R.id.house_price);
            houseAddress=itemView.findViewById(R.id.hose_address);
            houseLabel=itemView.findViewById(R.id.house_label);
        }
    }
}
